import card, { checkCard } from "./cardData/card"

type baseElement = {
    id: string
}
//修改此项时同时检查 function checkElement
export type element = cardElement | textboardElement
export type cardElement = baseElement & {
    type: 'card'
    card: card
}
export type textboardElement = baseElement & {
    type: 'textboard'
    text: string
}


export function cardElement(data: card): cardElement {
    return {
        id: '-1',
        type: 'card',
        card: data
    }
}

export function checkElement(obj: any): element | undefined {
    if (typeof obj == 'object') {
        if ('type' in obj && 'id' in obj) {
            let id: string
            if (typeof obj.id !== 'string') {
                id = obj.id?.toString()
            } else {
                id = obj.id
            }
            switch (obj.type) {
                case 'card': {
                    let card = checkCard(obj.card)
                    if (card != undefined) {
                        return {
                            id: id,
                            card: card,
                            type: 'card'
                        }
                    }
                    break;
                }
                case 'textboard': {
                    let text: string
                    if (typeof obj.text != 'string') {
                        text = obj.text?.toString() ?? ''
                    } else {
                        text = obj.text
                    }
                    return {
                        id: id,
                        text: obj.text,
                        type: 'textboard'
                    }
                }
            }
        }
    }
}